package com.excesys.exsecs.scada.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.scada.entity.ScadaSystem;
import com.excesys.exsecs.scada.mapper.ScadaSystemMapper;
import com.excesys.exsecs.scada.service.ScadaSystemService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 组态系统管理
 *
 * @author tzh
 * @date 2024-12-09 10:43:01
 */
@Service
@RequiredArgsConstructor
public class ScadaSystemServiceImpl extends ServiceImpl<ScadaSystemMapper, ScadaSystem> implements ScadaSystemService {

	private final ScadaSystemMapper scadaSystemMapper;
	@Override
	public Integer saveSystem(ScadaSystem scadaSystem) {

		String unitNo = UUID.randomUUID().toString().replace("-","");
		scadaSystem.setUnitNo(unitNo);
		Map map = new HashMap<>();
		map.put("name",scadaSystem.getSystemName());
		map.put("identifier",scadaSystem.getIdentifier());
		map.put("unit",scadaSystem.getUnit());
		map.put("datatype",scadaSystem.getDatatype());
		map.put("description",scadaSystem.getDescription());
		JSONObject jsonObject = new JSONObject(map);
		scadaSystem.setProperties(jsonObject);
		int insert = scadaSystemMapper.insert(scadaSystem);
		return insert;
	}

	@Override
	public List<Tree<Long>> systemTree(ScadaSystem scadaSystem) {
		List<TreeNode<Long>> collect = this.list(Wrappers.<ScadaSystem>lambdaQuery()
						.like(StrUtil.isNotBlank(scadaSystem.getSystemName()), ScadaSystem::getSystemName, scadaSystem.getSystemName())
						.eq(StrUtil.isNotBlank(scadaSystem.getType()), ScadaSystem::getType, scadaSystem.getType())
						.orderByAsc(ScadaSystem::getSort))
				.stream().map(getNodeFunction()).collect(Collectors.toList());
		return TreeUtil.build(collect,ObjectUtil.isNotNull(scadaSystem.getParentId())?scadaSystem.getParentId():-1L);
	}



	@NotNull
	private Function<ScadaSystem, TreeNode<Long>> getNodeFunction() {
		return system -> {
			TreeNode<Long> node = new TreeNode<>();
			node.setId(system.getId());
			node.setName(system.getSystemName());
			node.setParentId(system.getParentId());
			node.setWeight(system.getSort());
			//扩展属性
			Map<String,Object> extra = new HashMap<>();
			extra.put("type",system.getType().equals("0")?"文件夹":"系统参数");
			extra.put("remark",system.getRemark());
			if(ObjectUtil.isNotNull(system.getUnitNo())){
				extra.put("unitNo",system.getUnitNo());
				extra.put("properties",system.getProperties());
				JSONObject prop = system.getProperties();
				extra.put("unit",prop.get("unit"));
				extra.put("datatype",prop.get("datatype"));
				extra.put("identifier",prop.get("identifier"));
				extra.put("description",prop.get("description"));
			}
			node.setExtra(extra);
			return node;
		};
	}
}